package cn.edu.qfnu.oj.web.controller;

import cn.edu.qfnu.oj.entity.Problem;
import cn.edu.qfnu.oj.entity.Task;
import cn.edu.qfnu.oj.entity.Teacher;
import cn.edu.qfnu.oj.service.ProblemService;
import cn.edu.qfnu.oj.service.TaskService;
import cn.edu.qfnu.oj.service.TeacherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping(value = "task/")
public class TaskController {

    private static final Logger log = LoggerFactory.getLogger(TaskController.class);

    private final TaskService taskService;
    private final TeacherService teacherService;
    private final ProblemService problemService;

    @Autowired
    public TaskController(TaskService taskService, TeacherService teacherService, ProblemService problemService) {
        this.taskService = taskService;
        this.teacherService = teacherService;
        this.problemService = problemService;
    }

    @RequestMapping(value = "index2", method = RequestMethod.GET)
    public String createTask() {
        return "task";
    }

    @RequestMapping(value = "createTask", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> crateTask(String title, String content,
                                         String start, String duration, String term,
                                         List<Problem> problems, String teacherId) {

        //作业的持续时间duration：单位是天

        Map<String, String> jsonMap = new HashMap<>();

        //从字符串中获取开始时间
        try {
            LocalDateTime startTime = LocalDateTime.parse(start);

            //结束时间通过开始时间与结束时间相加
            LocalDateTime endTime = startTime.plusDays(Long.parseLong(duration));

            //创建新任务
            Task task = new Task(title, content, startTime, endTime, term);
            //通过遍历获得json中的problem_id
            for (Problem problem : problems) {
                //通过ID找到Problem
                Problem find = problemService.findById(problem.getId());
                //Problem加入题集
                find.getTasks().add(task);
                //更新题目记录
                problemService.saveOrUpdate(find);
                //题目集加入题目
                task.getProblems().add(find);
                log.info("题目：" + find.getTitle() + "加入到作业中");
            }

            Teacher teacher = teacherService.findById(teacherId);
            teacher.getTasks().add(task);
            teacherService.saveOrUpdate(teacher);
            //最后保存题目集
            task.setTeacher(teacher);
            taskService.save(task);
            jsonMap.put("result", "success");
            log.info("创建了一个新作业" + task.getTitle());
        } catch (Exception e) {
            jsonMap.put("result", "error");
            log.info("创建作业时出错", e);
        }
        return jsonMap;
    }

    @RequestMapping(value = "index3", method = RequestMethod.GET)
    public String modifyTask() {
        return "moTask";
    }

    @RequestMapping(value = "moTask", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> modifyTask(Integer id, String title, String content,
                                          String start, String duration, String term,
                                          @RequestBody Set<Problem> problems) {

        Map<String, String> jsonMap = new HashMap<>();

        try {
            Task task = taskService.findById(id);
            taskService.modify(task, title, content, start, duration, term, problems);
            taskService.merge(task);

            jsonMap.put("result", "success");
            log.info("修改了作业：" + task.getId() + " " + task.getTitle());
        } catch (Exception e) {
            jsonMap.put("result", "error");
            log.info("修改作业时候出错", e);
        }
        return jsonMap;
    }

    @RequestMapping(value = "index5", method = RequestMethod.GET)
    public String delete() {
        return "deleteTask";
    }

    @RequestMapping(value = "deleteTask", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Map<String, String> jsonMap = new HashMap<>();

        try {
            taskService.removeById(id);
            jsonMap.put("result", "success");
            log.info("删除了一个作业: " + id);
        } catch (Exception e) {
            jsonMap.put("result", "error");
            log.info("删除作业时出错", e);
        }
        return jsonMap;
    }


}
